#include "ShmMgr.h"
#include "SvrMemBlock.h"

CShmMgr::CShmMgr()
{
}

CShmMgr::~CShmMgr()
{
    Destroy();
}

void *CShmMgr::operator new(size_t sz)
{
    void *ptr = nullptr;
    CSvrMemBlock *smb = CSvrMemBlock::GetInstance();

    if (nullptr != smb)
        ptr = smb->Alloc(sz);

    return ptr;
}

void CShmMgr::operator delete(void *ptr)
{
    CSvrMemBlock *smb = CSvrMemBlock::GetInstance();

    if (nullptr != smb)
        smb->Free(ptr);
}

ShmNode *CShmMgr::CreateShmBlock(uint64_t nSize)
{
    ShmNode *pNode = (ShmNode *)m_lstShm.NewNode(nSize);

    if (nullptr != pNode)
    {
        pNode->Init((char *)pNode + sizeof(ShmNode), nSize);
        m_lstShm.PushBack(pNode);
    }

    return pNode;
}

void CShmMgr::DestroyShmBlock(ShmNode *pNode)
{
    m_lstShm.Remove(pNode);
    m_lstShm.DeleteNode(pNode);
}

void CShmMgr::Destroy()
{
    m_lstShm.Destroy();
}